package com.manage.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.manage.service.FlowerGoodsService;
import com.manage.service.FlowerLanguageService;
import com.manage.common.R;
import com.manage.dto.GoodsAndTypeDto;
import com.manage.pojo.FlowerGoods;
import com.manage.service.FlowerTypesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 商品信息
 */
@CrossOrigin // 跨域
@RestController
@RequestMapping("/flower-goods")
public class FlowerGoodsController {

    @Autowired
    private FlowerGoodsService flowerGoodsService;

    @Autowired
    private FlowerTypesService flowerTypesService;

    @Autowired
    private FlowerLanguageService flowerLanguageService;

    /**
     * 查询当前用户所有的商品
     * @param userId
     * @return
     */
    @GetMapping("/list")
    public R list(@RequestParam("userId") String userId){
        List<FlowerGoods> flowerGoods =  flowerGoodsService.goods_list(userId);
        return R.success(flowerGoods);
    }

    /**
     * 分页查询
     * @param current_page
     * @return
     */
    @GetMapping("/page")
    public R page(@RequestParam("current_page") Integer current_page, @RequestParam("userId") String userId){
        Page page = new Page<>(current_page,8);
        page = flowerGoodsService.pageList(page,userId);
        return R.success(page);
    }

    /**
     * 商品的搜索方法
     * @param goods_name
     * @return
     */
    @GetMapping("/search")
    public R search(@RequestParam("goods_name") String goods_name){
        List<FlowerGoods> list = flowerGoodsService.search_list(goods_name);
        return R.success(list,"查询成功！");
    }

    /**
     * 删除商品方法
     * @return
     */
    @DeleteMapping("/delete/{goodsId}")
    public R delete(@PathVariable("goodsId") String goodsId){
        boolean b = flowerLanguageService.removeById(goodsId);
        boolean flag = flowerGoodsService.removeById(goodsId);
        boolean bool = flowerTypesService.removeById(goodsId);
        if (flag && bool) {
            return R.success("删除成功！");
        }else {
            return R.error("删除失败！");
        }
    }

    /**
     * 更新当前商品的状态
     * @param goodsId
     * @param goodsStatus
     * @return
     */
    @PutMapping("/updateStatus/{goodsId}/{goodsStatus}")
    public R put(@PathVariable("goodsId") String goodsId,@PathVariable("goodsStatus") Integer goodsStatus){
        boolean flag = flowerGoodsService.updateByStatus(goodsId,goodsStatus);
        if (flag) {
            return R.success("修改成功！");
        }else {
            return R.error("修改失败！");
        }
    }

    /**
     * 查询商品和分类
     * @param goodsId
     * @return
     */
    @GetMapping("/update_list/{goodsId}")
    public R updateList(@PathVariable("goodsId") String goodsId) {
        GoodsAndTypeDto goodsDto = flowerGoodsService.select_update(goodsId);
        return R.success(goodsDto);
    }

    /**
     * 修改商品
     * @param dto
     * @return
     */
    @PutMapping("/update")
    public R update(@RequestBody GoodsAndTypeDto dto){
        String msg = flowerGoodsService.updateGoodsAndTypes(dto);
        return R.success(msg);
    }

    /**
     * 新增商品和商品分类
     * @param dto
     * @return
     */
    @PostMapping("/insert")
    public R insert(@RequestBody GoodsAndTypeDto dto){
        boolean bool = flowerGoodsService.setGoodsAndType(dto);
        if (bool){
            return R.success("新增成功！");
        }else {
            return R.error("新增失败");
        }
    }
}

